<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText" @gobackTriser="goBack"></topTitle>
    <div class="mainCon">
      <el-row
        class="toolbar searchInputWidth"
        style="padding-bottom: 0; margin-bottom: 0"
      >
        <el-form :inline="true" :model="filterField">
          <el-col :lg="8" :xl="6">
            <el-form-item label="字典名称" class="row-padding-bottom">
              <el-input
                placeholder="请输入内容"
                v-model="filterField.dicitemName"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="字典类型" class="row-padding-bottom">
              <el-input
                placeholder="请输入内容"
                v-model="filterField.dicitemType"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="是否删除" class="row-padding-bottom">
                <el-select
                  v-model="filterField.isDeleted"
                  clearable
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) of isDeletedData"
                    :key="index"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="标签" class="row-padding-bottom">
                <el-input
                  placeholder="请输入内容"
                  v-model="filterField.laber"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
              <exportBtn
                v-if="!isShowAdd"
                :dataLen="tableList.length"
                :filterField="filterField"
                api="api-s/report/export_SYS_DICT"
              ></exportBtn>
              <!-- <el-button class="commonBut saveBtn funComBut"  icon="icon iconfont icontianjia"  @click="addCashDeposit" v-show="isShowAdd">
                <span class="opetareText">添加</span>
              </el-button> -->
            </el-form-item>
          </el-col>
          <!-- <el-form-item>
              <el-button type="primary" icon="icon iconfont iconchazhao1" @click="searchList" class="commonBtn"></el-button>
            </el-form-item> -->
          <!-- <el-form-item>
              <el-button class="commonBut saveBtn funComBut"  icon="icon iconfont icontianjia"  @click="addCashDeposit" v-show="isShowAdd">
                <span class="opetareText">添加</span>
              </el-button>
            </el-form-item> -->
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px" v-if="isShowAdd">
          <exportBtn
            :dataLen="tableList.length"
            :filterField="filterField"
            api="api-s/report/export_SYS_DICT"
          ></exportBtn>
          <el-button
            class="commonBut saveBtn funComBut"
            icon="icon iconfont icontianjia"
            @click="addCashDeposit"
          >
            <span class="opetareText">添加</span>
          </el-button>
        </div>
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableList"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          v-loading="loading"
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="id"
            label="id"
            align="center"
            width="300"
          ></el-table-column>
          <el-table-column
            prop="dicitemType"
            label="字典类型"
            align="center"
            width="200"
          ></el-table-column>
          <el-table-column
            prop="dicitemId"
            label="字典编码"
            align="center"
            width="150"
          ></el-table-column>
          <!-- <el-table-column prop="dicitemName" label="字典名称" align="center"></el-table-column>   -->
          <el-table-column label="字典名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.dicitemName"
                :columnName="'dicitemName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="laber"
            label="标签"
            align="center"
            width="150"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="remark"
            label="备注"
            align="center"
            width="200"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="orderNo"
            label="排序"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="updateTime"
            label="修改时间"
            align="center"
            width="200"
          ></el-table-column>
          <el-table-column
            prop="isDeleted"
            label="是否删除"
            align="center"
            width="150"
          >
            <template slot-scope="scope">
              {{ scope.row.isDeleted == 0 ? '是' : '否' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="left" width="180" fixed="right">
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="editCashDepositBank(scope.row)"
                v-show="isShowUpdate"
                >修改</el-link
              >
              <el-link
                type="primary"
                @click="toDel(scope.row)"
                v-show="isShowDel"
                >删除</el-link
              >
              <el-link
                type="primary"
                @click="toCopy(scope.row)"
                v-show="isShowAdd"
                >复制</el-link
              >
              <span v-show="!isShowAdd && !isShowUpdate && !isShowDel">--</span>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
        ></Pagination>
      </div>
    </div>
    <el-dialog
      :title="addOrEdit == 0 ? '添加字典' : '修改字典'"
      :visible.sync="formVisible"
      :close-on-click-modal="false"
      @close="cancel('formData')"
      custom-class="brand-dialog-min-width"
      :modal-append-to-body="false"
      :append-to-body="true"
    >
      <el-form :model="formData" :rules="formRules" ref="formData">
        <el-form-item
          label="字典标签"
          :label-width="formLabelCardWidth"
          prop="laber"
        >
          <el-input
            placeholder="请输入内容"
            v-model="formData.laber"
            clearable
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item
          label="字典类型"
          :label-width="formLabelCardWidth"
          prop="dicitemType"
        >
          <el-input
            placeholder="请输入内容"
            v-model="formData.dicitemType"
            clearable
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item
          label="字典编码"
          :label-width="formLabelCardWidth"
          prop="dicitemId"
        >
          <el-input
            placeholder="请输入内容"
            v-model="formData.dicitemId"
            clearable
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item
          label="字典名称"
          :label-width="formLabelCardWidth"
          prop="dicitemName"
        >
          <el-input
            placeholder="请输入内容"
            v-model="formData.dicitemName"
            clearable
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item
          label="备注"
          :label-width="formLabelCardWidth"
          prop="remark"
        >
          <el-input
            placeholder="请输入内容"
            v-model="formData.remark"
            clearable
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item
          label="排序"
          :label-width="formLabelCardWidth"
          prop="orderNo"
        >
          <!-- <el-input placeholder="请输入内容" v-model="formData.orderNo" clearable></el-input> -->
          <el-input-number
            v-model="formData.orderNo"
            :min="0"
            :max="999"
            label="排序"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="cancel('formData')" class="cancel"
          >取消</el-button
        >
        <el-button
          type="primary"
          @click.native="saveBankInfo('formData')"
          v-show="isShowSave"
          class="save"
          :loading="canLogin"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
export default {
  name: 'dictionariesManage',
  mixins: [scrollTable],
  data() {
    return {
      isShow: false,
      titleText: '字典管理',
      advanced: false,
      //表格数据
      tableList: [],
      requestUrl: 'api-bs/base/dicitem/listbypage',
      time: [],
      areaId: '',
      addAreaId: '',
      //搜索条件
      filterField: {
        dicitemName: '',
        dicitemType: '',
        isDeleted: '1',
      },
      pageSize: 20,
      pageNo: 1,
      isDeletedData: [
        {
          code: '0',
          name: '是',
        },
        {
          code: '1',
          name: '否',
        },
      ],
      loading: false,
      formVisible: false,
      //   弹窗数据
      // dicitemType  laber  dicitemId  dicitemName  orderNo  remark  isDeleted
      formData: {
        id: '',
        dicitemType: '',
        laber: '',
        dicitemId: '',
        dicitemName: '',
        orderNo: '',
        remark: '',
      },
      formRules: {
        laber: [
          {
            required: true,
            message: '字典标签不能为空',
            trigger: 'blur',
          },
        ],
        dicitemType: [
          {
            required: true,
            message: '字典类型不能为空',
            trigger: 'blur',
          },
        ],
        dicitemId: [
          {
            required: true,
            message: '字典编码不能为空',
            trigger: 'blur',
          },
        ],
        dicitemName: [
          {
            required: true,
            message: '字典名称不能为空',
            trigger: 'blur',
          },
        ],
      },
      formLabelCardWidth: '125px',
      canLogin: false,
      addOrEdit: 0,
      tabPosition: 'setCashDeposit',
      isShowDel: '',
      isShowUpdate: '',
      isShowAdd: '',
      isShowSave: '',
    }
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.isShowAdd =
      this.$store.state.buttonLimitsArr.indexOf('dicManage:add') > -1
    this.isShowUpdate =
      this.$store.state.buttonLimitsArr.indexOf('dicManage:update') > -1
    this.isShowDel =
      this.$store.state.buttonLimitsArr.indexOf('dicManage:delete') > -1
    this.isShowSave =
      this.$store.state.buttonLimitsArr.indexOf('dicManage:add') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('dicManage:update') > -1
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    addCashDeposit() {
      this.addOrEdit = 0
      this.formVisible = true
      this.addAreaId = ''

      // dicitemType  laber  dicitemId  dicitemName  orderNo  remark  isDeleted
      this.formData = {
        dicitemType: '',
        laber: '',
        dicitemId: '',
        dicitemName: '',
        orderNo: '',
        remark: '',
        id: '',
      }

      // 去除之前的验证
      this.$nextTick(() => {
        this.$refs.formData.clearValidate()
      })
    },
    editCashDepositBank(row) {
      this.addOrEdit = 1
      this.formVisible = true
      this.formData = {
        dicitemType: row.dicitemType,
        laber: row.laber,
        dicitemId: row.dicitemId,
        dicitemName: row.dicitemName,
        orderNo: row.orderNo,
        remark: row.remark,
        id: row.id,
      }
    },
    toCopy(row) {
      this.addOrEdit = 0
      this.formVisible = true
      this.formData = {
        dicitemType: row.dicitemType,
        laber: row.laber,
        dicitemId: '',
        dicitemName: '',
        orderNo: '',
        remark: '',
        id: '',
      }
    },
    saveBankInfo() {
      this.canLogin = true
      this.$refs.formData.validate((valid) => {
        if (valid) {
          if (this.addOrEdit == '0') {
            this.toAddBankInfo()
          } else {
            this.toUpdateBankInfo()
          }
        } else {
          this.canLogin = false
        }
      })
    },
    toAddBankInfo() {
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/adddicitem',
        data: this.formData,
      })
        .then(
          (response) => {
            this.canLogin = false
            if (!response.data.hasError) {
              this.$message({
                message: '添加成功',
                type: 'success',
              })
              this.formVisible = false
              this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
          },
          (response) => {
            this.canLogin = false
          }
        )
        .catch((error) => {
          this.canLogin = false
        })
    },
    toUpdateBankInfo() {
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/updatedicitem',
        data: this.formData,
      })
        .then(
          (response) => {
            this.canLogin = false
            if (!response.data.hasError) {
              this.$message({
                message: '修改成功',
                type: 'success',
              })
              this.formVisible = false
              this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
          },
          (response) => {
            this.canLogin = false
          }
        )
        .catch((error) => {
          this.canLogin = false
        })
    },
    cancel(formData) {
      this.$refs['formData'].resetFields()
      this.formVisible = false
    },
    toDel(row) {
      this.$confirm('确认删除?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        type: 'warning',
      })
        .then(() => {
          this.del(row)
        })
        .catch(() => {
          // this.$message.error("删除失败");
        })
    },
    del(row) {
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/deletedicitem',
        data: {
          id: row.id,
        },
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.$message({
                message: '删除成功',
                type: 'success',
              })
              this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
  },
}
</script>

<style scoped lang="less">
@import '../../../static/css/filterArea.less';
.mainCon {
  top: 196px;
}
</style>
